package com.yungu.swift.api.route.v2.passenger;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yungu.swift.assets.passenger.model.dto.PassengerDto;
import com.yungu.swift.assets.passenger.service.PassengerService;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.system.marketing.model.dto.MarketingActivityDto;
import com.yungu.swift.system.marketing.model.param.ApiMarketingParam;
import com.yungu.swift.system.marketing.service.MarketingActivityService;
import com.yungu.swift.system.sys.model.dto.SysAreaDto;
import com.yungu.swift.system.sys.service.SysAreaService;
import com.yungu.swift.system.sys.service.SysCityService;
import com.yungu.swift.utils.MapUtils;
import com.yungu.swift.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

import static com.yungu.swift.api.base.ApiErrorEnum.PARAM_ERROR;

/**
 * @author : cuixiuyin
 * @date : 2019/12/9
 */
@Slf4j
@RestController
@RequestMapping("/api/v2/passenger/marketing")
@Api(value = "【乘客】【营销】乘客活动接口V2", tags = "V2-乘客活动接口")
public class PasMarketingApi {

    @Reference
    private SysCityService sysCityService;
    @Reference
    private SysAreaService sysAreaService;
    @Reference
    private PassengerService passengerService;
    @Reference
    private MarketingActivityService marketingActivityService;


    @PostMapping(value = {"/activity/list"})
    @ApiOperation(value = "获取活动信息", notes = "获取活动信息")
    public ResponseData<List<MarketingActivityDto>> listActivity(@RequestBody ApiMarketingParam marketingParam) {
        Map<String, Object> paramMap = MapUtils.build();
        paramMap.put("appid", marketingParam.getAppid());
        paramMap.put("businessType", marketingParam.getBusinessType());
        paramMap.put("adCode", marketingParam.getAdCode());
        paramMap.put("showType", marketingParam.getShowType());
        //为空也传，为空就取全国发放的
        if (StringUtils.isEmpty(marketingParam.getAdCode())) {
            paramMap.put("adCodeNull", 1);
        }
        //不为空，查询相关城市
        if (StringUtils.isNotEmpty(marketingParam.getAdCode())) {
            String cityUuid = sysCityService.getCityUuidByAdCode(marketingParam.getAdCode()).getData();
            if (StringUtils.isEmpty(cityUuid)) {
                SysAreaDto sysAreaDto = sysAreaService.get(MapUtils.build("areaID", marketingParam.getAdCode())).getData();
                if (sysAreaDto != null) {
                    cityUuid = sysCityService.getCityUuidByAdCode(sysAreaDto.getFather()).getData();
                }
            }
            paramMap.put("paramCityUuid", cityUuid);
            // 更新乘客信息
            final PassengerDto passengerParam = new PassengerDto();
            passengerParam.setUuid(marketingParam.getUserUuid());
            passengerParam.setCurrentAdCode(marketingParam.getAdCode());
            passengerParam.setCityUuid(cityUuid);
            CompletableFuture.runAsync(() -> passengerService.edit(passengerParam));
        }
        return marketingActivityService.getActivities(paramMap);
    }

    @PostMapping(value = {"/activity/countActivity"})
    @ApiOperation(value = "增加活动点击量", notes = "增加活动点击量")
    public ResponseData<Boolean> countActivity(@RequestBody Map<String, String> paramMap) {
        // 活动uuid
        String activityUuid = paramMap.get("activityUuid");
        if (StringUtils.isEmpty(activityUuid)) {
            return ResponseData.buildErrorResponse(PARAM_ERROR.getCode(), PARAM_ERROR.getMsg());
        }
        return marketingActivityService.updateCilckCount(activityUuid);
    }
}
